using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Sheep_Breeding.ErrorCode;
using Sheep_Breeding.Write.Api.OperationServices.IServices;
using Sheep_Breeding.Write.Api.production_a_DTO;

namespace Sheep_Breeding.Write.Api.Controllers
{
    /// <summary>
    /// 母羊配种管理控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class EweMatingController : ControllerBase
    {
        private readonly ILogger<EweMatingController> _logger;
        private readonly IEweMatingServices _eweMatingServices;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger">日志记录器</param>
        /// <param name="eweMatingServices">母羊配种服务</param>
        public EweMatingController(
            ILogger<EweMatingController> logger,
            IEweMatingServices eweMatingServices)
        {
            _logger = logger;
            _eweMatingServices = eweMatingServices;
        }

        /// <summary>
        /// 新增母羊配种记录
        /// </summary>
        /// <param name="dto">母羊配种新增DTO</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        // [Authorize] // 临时注释掉授权要求，用于测试
        public async Task<ApiResult> AddEweMating([FromBody] AddEweMatingDto dto)
        {
            try
            {
                _logger.LogInformation($"开始新增母羊配种记录，耳号：{dto.EarNumber}");
                
                // 调用服务层方法
                var result = await _eweMatingServices.AddEweMating(dto);
                
                _logger.LogInformation($"新增母羊配种记录完成，耳号：{dto.EarNumber}，结果：{result.msg}");
                
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError($"新增母羊配种记录异常：{ex.Message}，耳号：{dto.EarNumber}");
                throw;
            }
        }

        /// <summary>
        /// 批量新增母羊配种
        /// </summary>
        [HttpPost]
        public async Task<ApiResult> AddEweMatingBatch([FromBody] AddEweMatingBatchDto batchDto)
        {
            return await _eweMatingServices.AddEweMatingBatch(batchDto);
        }

        /// <summary>
        /// 主表+明细表联合添加
        /// </summary>
        [HttpPost]
        public async Task<ApiResult> AddEweMatingWithDetails([FromBody] AddEweMatingWithDetailsDto dto)
        {
            return await _eweMatingServices.AddEweMatingWithDetails(dto);
        }

        /// <summary>
        /// 编辑母羊配种记录
        /// </summary>
        /// <param name="dto">编辑DTO</param>
        /// <returns>操作结果</returns>
        [HttpPut]
        public async Task<ApiResult> UpdateEweMating([FromBody] UpdateEweMatingDto dto)
        {
            try
            {
                _logger.LogInformation($"开始编辑母羊配种记录，配种编号：{dto.BreedingId}");
                var result = await _eweMatingServices.UpdateEweMating(dto);
                _logger.LogInformation($"编辑母羊配种记录完成，配种编号：{dto.BreedingId}，结果：{result.msg}");
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError($"编辑母羊配种记录异常：{ex.Message}，配种编号：{dto.BreedingId}");
                throw;
            }
        }

        /// <summary>
        /// 根据配种编号进行逻辑删除
        /// </summary>
        /// <param name="breedingId">配种编号</param>
        /// <returns>操作结果</returns>
        [HttpDelete]
        public async Task<ApiResult> DeleteEweMating([FromQuery] string breedingId)
        {
            try
            {
                _logger.LogInformation($"开始逻辑删除母羊配种记录，配种编号：{breedingId}");
                var result = await _eweMatingServices.DeleteEweMating(breedingId);
                _logger.LogInformation($"逻辑删除母羊配种记录完成，配种编号：{breedingId}，结果：{result.msg}");
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError($"逻辑删除母羊配种记录异常：{ex.Message}，配种编号：{breedingId}");
                throw;
            }
        }
    }
} 